package com.thinkgem.jeesite.modules.task.common.taskjob;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.thinkgem.jeesite.modules.cn.entity.AttendanceCn;
import com.thinkgem.jeesite.modules.cn.service.AttendanceCnService;
import com.thinkgem.jeesite.modules.employee.service.AttendanceRecordsService;
import com.thinkgem.jeesite.modules.us.entity.UsAttendanceAdp;
import com.thinkgem.jeesite.modules.us.service.UsAttendanceAdpService;
import com.thinkgem.jeesite.common.utils.Constons;

/**
 * Created by pan.zhou on 2017/7/24.
 */
@Component("statisticalUsAttendanceTask")
public class StatisticalUsAttendanceTask {
    @Autowired
    private UsAttendanceAdpService usAttendanceAdpService;
    @Autowired
    private AttendanceCnService attendanceCnService;
    @Autowired
    private AttendanceRecordsService attendanceRecordsService;

    public void doSomeThing(){
        //查询正岗列表中美东仓的考勤数据
        List<UsAttendanceAdp> adps = usAttendanceAdpService.queryByWarehouse(Constons.AttanceWarehouse.USKYN.getWarehouseName());
        List<AttendanceCn> adpList = convertPropoties(adps);
        AttendanceCn condition = new AttendanceCn();
        condition.setWarehouse(Constons.AttanceWarehouse.USKYN.getWarehouseName());
        List<AttendanceCn> attendanceUskyn = attendanceCnService.findListByDate(condition);
        //美东仓员工考勤总数
        List<AttendanceCn> uskynList = new ArrayList<AttendanceCn>();        
        uskynList.addAll(adpList);        
        if(attendanceUskyn!=null){
            uskynList.addAll(attendanceUskyn);
        }        
        
        //查询正岗列表中美西仓的考勤数据
        List<UsAttendanceAdp> adpUskyn = usAttendanceAdpService.queryByWarehouse(Constons.AttanceWarehouse.USWC.getWarehouseName());
        List<AttendanceCn> adpUskynList= convertPropoties(adpUskyn);
        condition.setWarehouse(Constons.AttanceWarehouse.USWC.getWarehouseName());
        List<AttendanceCn> attendanceUswc = attendanceCnService.findListByDate(condition);
        
        //美东仓员工考勤总数
        List<AttendanceCn> uswcList = new ArrayList<AttendanceCn>(); 
        uswcList.addAll(adpUskynList);
        if(attendanceUswc!=null){
            uswcList.addAll(attendanceUswc);
        }
        
        //德国仓
        condition.setWarehouse(Constons.AttanceWarehouse.DEBR.getWarehouseName());
        List<AttendanceCn> attendanceDebr = attendanceCnService.findListByDate(condition);
        if(attendanceDebr!=null){
            uswcList.addAll(attendanceDebr);
        }
        //澳洲仓
        condition.setWarehouse(Constons.AttanceWarehouse.AUSY.getWarehouseName());
        List<AttendanceCn> attendanceAusy = attendanceCnService.findListByDate(condition);
        if(attendanceAusy!=null){
            uswcList.addAll(attendanceAusy);
        }
        //上海仓
        condition.setWarehouse(Constons.AttanceWarehouse.ShangHai.getWarehouseName());
        List<AttendanceCn> attendanceShangHai = attendanceCnService.findListByDate(condition);
        if(attendanceShangHai!=null){
            uswcList.addAll(attendanceShangHai);
        }
        
        //深圳仓
        condition.setWarehouse(Constons.AttanceWarehouse.ShenZhen.getWarehouseName());
        List<AttendanceCn> attendanceShenZhen = attendanceCnService.findListByDate(condition);
        if(attendanceShenZhen!=null){
            uswcList.addAll(attendanceShenZhen);
        }
        
        //广州仓
        condition.setWarehouse(Constons.AttanceWarehouse.GuangZhou.getWarehouseName());
        List<AttendanceCn> attendanceGuangZhou = attendanceCnService.findListByDate(condition);
        if(attendanceGuangZhou!=null){
            uswcList.addAll(attendanceGuangZhou);
        }
        
        //根据倒入的工时对接处理
        attendanceRecordsService.generateAllAttendance(uskynList);
        
        //根据法定节假日和请假生成考勤
        attendanceRecordsService.generateLeaveAttendance();
    }
    
    /**
     * 
     * 对象转换，正岗转仓库类型员工
     * @param adpList
     * @return list<AttendanceCn>
     */
    private List<AttendanceCn>  convertPropoties(List<UsAttendanceAdp> adpList){
        List<AttendanceCn> list = new ArrayList<AttendanceCn>();
        if(adpList!=null&&adpList.size()>0){
            for (UsAttendanceAdp usAttendanceAdp : adpList) {
                AttendanceCn attendanceCn = new AttendanceCn();
                attendanceCn.setNumber(usAttendanceAdp.getEmployeeId());
                attendanceCn.setDate(usAttendanceAdp.getInTime());
                attendanceCn.setWorkingHours(usAttendanceAdp.getHours());
                attendanceCn.setWarehouse(usAttendanceAdp.getWarehouse());
                attendanceCn.setCompanyId(usAttendanceAdp.getCompanyId());
                attendanceCn.setOfficeId(usAttendanceAdp.getOfficeId());
                list.add(attendanceCn);
            }    
        }
        return list;
    }
}
